home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacButtonUI.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  322 lines

  1. /*
  2.  * @(#)MacButtonUI.java    1.6 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.mac;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import com.sun.java.swing.plaf.*;
  29.  
  30. /**
  31.  * <p>
  32.  * Warning: serialized objects of this class will not be compatible with
  33.  * future swing releases.  The current serialization support is appropriate
  34.  * for short term storage or RMI between Swing1.0 applications.  It will
  35.  * not be possible to load serialized Swing1.0 objects with future releases
  36.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  37.  * baseline for the serialized form of Swing objects.
  38.  *
  39.  * @version @(#)MacButtonUI.java    1.0 11/24/97
  40.  * @author Symantec
  41.  * @author Levi Brown
  42.  */
  43.  
  44. public class MacButtonUI extends BasicButtonUI
  45. {
  46.     /**
  47.     * border & margin
  48.     */
  49.     protected final static Insets defaultMargin = new Insets(0, 8, 0, 8);
  50.     protected final static Insets defaultIconMargin = new Insets(1, 1, 1, 1);
  51.     protected final static Insets defaultCombinedMargin = new Insets(2, 4, 2, 4);
  52.     
  53.     protected MacButtonListener macButtonListener = new MacButtonListener(null);
  54.  
  55.     protected static Color GSBColor = UIManager.getColor("GrayscaleAppearanceB");
  56.     protected static Color GSWColor = UIManager.getColor("GrayscaleAppearanceW");
  57.     protected static Color GS2Color = UIManager.getColor("GrayscaleAppearance2");
  58.     protected static Color GS7Color = UIManager.getColor("GrayscaleAppearance7");
  59.     protected static Color GS9Color = UIManager.getColor("GrayscaleAppearance9");
  60.  
  61.     protected ActivationHelper activationHelper;
  62.     protected ChangeListener changeListener;
  63.     protected boolean isActive = true;
  64.     
  65.     public static ComponentUI createUI(JComponent c)
  66.     {
  67.         return new MacButtonUI();
  68.     }
  69.     
  70.     public void installUI(JComponent c)
  71.     {
  72.         super.installUI(c);
  73.         
  74.         //The Mac doesn't paint the focus...
  75.         ((AbstractButton)c).setFocusPainted(false);
  76.         
  77.         LookAndFeel.installBorder(c,"Button.border");
  78.     }
  79.  
  80.     public boolean isActive()
  81.     {
  82.         return isActive;
  83.     }
  84.  
  85.     public Dimension getPreferredSize(JComponent c) {
  86.         AbstractButton b = (AbstractButton)c;
  87.         return MacGraphicsUtils.getPreferredButtonSize(b, getDefaultTextIconGap(b));
  88.     }
  89.  
  90.     public Insets getDefaultMargin(AbstractButton b)
  91.     {
  92.         String text = b.getText();
  93.         if (text != null && !text.equals("")) {
  94.             if (b.getIcon() != null)
  95.                 return defaultCombinedMargin;
  96.             return defaultMargin;
  97.         } else
  98.             return defaultIconMargin;
  99.     }
  100.     
  101.     public boolean contains(JComponent c, int x, int y)
  102.     {
  103.         boolean isDefault = false;
  104.         
  105.         if(c instanceof JButton)
  106.         {
  107.             JButton b = (JButton)c;
  108.             isDefault = b.isDefaultButton();
  109.         }
  110.         
  111.         if(isDefault)
  112.         {
  113.             try
  114.             {
  115.                 MacButtonBorder    border    = (MacButtonBorder) c.getBorder();
  116.                 Insets            i        = border.getDefaultBorderInsets(c);
  117.                 Rectangle        rect    = c.getBounds();
  118.                 
  119.                 rect.x        += i.left;
  120.                 rect.y        += i.top;
  121.                 rect.width    -= i.left + i.right;
  122.                 rect.height    -= i.top + i.bottom;
  123.                 
  124.                 return rect.contains(x, y);;
  125.             }
  126.             catch(ClassCastException exc) {}
  127.         }
  128.  
  129.         return super.contains(c, x, y);;
  130.     }
  131.  
  132.     public void paint(Graphics g, JComponent c)
  133.     {
  134.         AbstractButton b = (AbstractButton) c;
  135.         ButtonModel model = b.getModel();
  136.         
  137.         if (model.isArmed() && model.isPressed())
  138.         {
  139.             paintButtonPressed(g,b); 
  140.         }
  141.         else
  142.         {
  143.             paintButton(g, b, false);
  144.         }
  145.     }
  146.     
  147.     protected void paintButtonPressed(Graphics g, AbstractButton b)
  148.     {
  149.         paintButton(g, b, true);
  150.     }
  151.     
  152.     protected void paintButton(Graphics g, AbstractButton b, boolean isPressed)
  153.     {
  154.         JComponent    c            = (JComponent) b;
  155.         ButtonModel    model        = b.getModel();
  156.         Dimension    size        = b.getSize();
  157.         FontMetrics    fm            = g.getFontMetrics();
  158.         Rectangle    viewRect    = new Rectangle(size);
  159.         Rectangle    iconRect    = new Rectangle();
  160.         Rectangle    textRect    = new Rectangle();
  161.         Color        fillColor;
  162.         Color        textColor;
  163.         
  164.         if(model.isEnabled() && isActive())
  165.         {
  166.             if(isPressed)
  167.             {
  168.                 fillColor = GS9Color;
  169.                 textColor = GSWColor;
  170.             }
  171.             else
  172.             {
  173.                 fillColor = GS2Color;
  174.                 textColor = GSBColor;
  175.             }
  176.         }
  177.         else
  178.         {
  179.                 fillColor = GS2Color;
  180.                 textColor = GS7Color;
  181.         }
  182.  
  183.         // Paint background
  184.         g.setColor(fillColor);
  185.         g.fillRect(2,2,size.width-5,size.height-5);
  186.  
  187.         // layout the text and icon
  188.         String text = SwingUtilities.layoutCompoundLabel
  189.         (
  190.             fm, b.getText(), b.getIcon(),
  191.             b.getVerticalAlignment(), b.getHorizontalAlignment(),
  192.             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  193.             viewRect, iconRect, textRect, b.getText() == null ? 0 : getDefaultTextIconGap(b)
  194.         );
  195.     
  196.         //Adjust the rectangles for the Mac
  197.         if(b.getVerticalAlignment() == AbstractButton.CENTER)
  198.         {
  199.             --textRect.x;
  200.             --textRect.y;
  201.             --iconRect.y;
  202.         }
  203.         
  204.         g.setColor(textColor);
  205.  
  206.          // Draw the icon if needed
  207.          paintIcon(g, b, iconRect, isPressed);
  208.     
  209.         // Draw the Text if needed
  210.          paintText(g, b, textRect, text, textColor);
  211.  
  212.         // Paint the focus if needed (shouldn't be, but leaving in for extensibility)
  213.         if (b.isFocusPainted() && b.hasFocus())
  214.         {
  215.             // paint UI specific focus
  216.             paintFocus(g, size);
  217.         }
  218.     }
  219.  
  220.      protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text, Color textColor)
  221.      {
  222.         // Draw the Text
  223.         if(text != null && !text.equals(""))
  224.         {
  225.             JComponent    c        = (JComponent) b;
  226.             Font        f        = c.getFont();
  227.             g.setFont(f);
  228.             FontMetrics    fm        = g.getFontMetrics();
  229.             
  230.             g.drawString(text, textRect.x, textRect.y + fm.getAscent() + ((fm.getLeading() + 1) / 2));
  231.         }        
  232.      }
  233.      
  234.      protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect, boolean isPressed)
  235.      {
  236.         // Paint the Icon
  237.         if(b.getIcon() != null)
  238.         { 
  239.             JComponent    c        = (JComponent) b;
  240.             ButtonModel    model    = b.getModel();
  241.             Icon        icon    = null;
  242.             
  243.             if(!model.isEnabled())
  244.             {
  245.                 icon = (Icon) b.getDisabledIcon();
  246.             }
  247.             else if(isPressed)
  248.             {
  249.                 icon = (Icon) b.getPressedIcon();
  250.                 // if no pressed icon, draw a darker version of the icon
  251.                 if (icon == null)
  252.                 {
  253.                     // Use default icon
  254.                     icon = (Icon) b.getIcon();
  255.                     if (icon != null && icon instanceof ImageIcon) {
  256.                         icon = new ImageIcon(
  257.                             PressedFilter.createPressedImage(((ImageIcon) icon).getImage()));
  258.                     }
  259.                 }
  260.             }
  261.             else if(b.isRolloverEnabled() && model.isRollover())
  262.             {
  263.                 icon = (Icon) b.getRolloverIcon();
  264.             }
  265.         
  266.             if (icon == null)
  267.             {
  268.                 icon = (Icon) b.getIcon();
  269.             }
  270.             
  271.             icon.paintIcon(c, g, iconRect.x, iconRect.y);
  272.         }
  273.     }
  274.  
  275.     protected void paintFocus(Graphics g, Dimension size)
  276.     {
  277.         //Do nothing
  278.     }
  279.  
  280.     protected BasicButtonListener createListener(JComponent c)
  281.     {
  282.         return macButtonListener;
  283.     }
  284.     
  285.     protected void installListeners(JComponent c)
  286.     {
  287.         super.installListeners(c);
  288.         
  289.         activationHelper    = new ActivationHelper(c);
  290.         changeListener        = new ChangeListener();
  291.         activationHelper.addPropertyChangeListener(changeListener);
  292.     }
  293.     
  294.     protected void uninstallListeners(JComponent c)
  295.     {
  296.         super.uninstallListeners(c);
  297.         activationHelper.removePropertyChangeListener(changeListener);
  298.         activationHelper = null;
  299.         changeListener = null;
  300.     }
  301.  
  302.     class ChangeListener implements java.beans.PropertyChangeListener
  303.     {
  304.         public void propertyChange(java.beans.PropertyChangeEvent evt)
  305.         {
  306.             if(evt.getPropertyName().equals("activated"))
  307.             {
  308.                 boolean oldActive = isActive;
  309.                 isActive = ((Boolean) evt.getNewValue()).booleanValue();
  310.                 if(isActive != oldActive)
  311.                 {
  312.                     Object obj = evt.getSource();
  313.                     if(obj instanceof ActivationHelper)
  314.                     {
  315.                         ((ActivationHelper)obj).getComponent().repaint();
  316.                     }
  317.                 }
  318.             }
  319.         }
  320.     }
  321. }
  322.